<?php

namespace app\api\model;

use think\Model;
use think\Db;
use think\db\Where;

class HousingResources extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ty_housing_resources';
    /**
     * 获取房源详情列表
     */
    public static  function getList($param)
    {
        $where = [];
        if(!empty($param)){
            foreach ($param as $k => $v){
                $param[$k] = '%'.$v.'%';
            }
            $where = array('rq_name','like',$param,'and');
        }
        $idList = ResidentialQuarters::getIdList($where);
        $where2[] = array('hr_rq_id','in',$idList);;
        $list =   Db::table('ty_housing_resources')
            ->field('hr_id,hr_city,hr_rq_id,hr_entrance,hr_storey,hr_house_number')
            ->where([$where2])
            ->select();
        foreach ($list as $key => $val){
            $rq_name = ResidentialQuarters::getRqName($val['hr_rq_id']);
            $list[$key]['con_house_gyxxdz'] = $val['hr_city'].'-'.$rq_name.'-'.$val['hr_entrance'].'-'.$val['hr_storey'].'-'.$val['hr_house_number'];
            $list[$key]['tch_id'] = $val['hr_id'];
            unset($list[$key]['hr_id'],$list[$key]['hr_city'],$list[$key]['hr_rq_id'],$list[$key]['hr_entrance'],$list[$key]['hr_storey'],$list[$key]['hr_house_number']);
        }
        return $list;
    }
    /**
     * 获取当前房源的年度总用量
     * 返回 当前阶梯电价
     */
    public static  function getUnitPrice($where)
    {
        $info =   Db::table('ty_housing_resources')
            ->field('hr_statistics')
            ->where($where)
            ->find();
        if($info['hr_statistics'] < 2160){
            return config('constantdata.ElecTariff')['firstOrder'];
        }elseif($info['hr_statistics'] > 4800){
            return config('constantdata.ElecTariff')['threeOrder'];
        }else{
            return config('constantdata.ElecTariff')['secondOrder'];
        }
    }
    /**
     * 更新当前房源的年度总用量
     * 返回 结果
     */
    public static  function updateStatistics($where,$change)
    {
        $info = self::where($where)->field('hr_statistics')->find();
        $update['hr_statistics'] = $info['hr_statistics'] + $change;
        $res =   Db::table('ty_housing_resources')->where($where)->update($update);
        ////日志
        $logContent = "\r\n";
        $logContent .= "Datetime: " . date("Y-m-d H:i:s") . ' time: ' . time() . "\r\n";
        $logContent .= "update: " . json_encode($update) . "\r\n";
        $logContent .= "change: " . $change . "\r\n";
        $logContent .= "Res: " . json_encode($res) . "\r\n";
        self::writeLog($logContent, "updateStatistics");
        return $res;
    }
}
